home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / mui20dev.lha / MUI / Developer / C / Examples / Class3.c < prev    next >
C/C++ Source or Header  |  1994-02-11  |  8KB  |  305 lines

  1. #include "demo.h"
  2.  
  3.  
  4. /***************************************************************************/
  5. /* Here is the beginning of our simple new class...                        */
  6. /***************************************************************************/
  7.  
  8. /*
  9. ** This is an example for the simplest possible MUI class. It's just some
  10. ** kind of custom image and supports only two methods:
  11. ** MUIM_AskMinMax and MUIM_Draw.
  12. */
  13.  
  14. /*
  15. ** This is the instance data for our custom class.
  16. ** Since it's a very simple class, it contains just a dummy entry.
  17. */
  18.  
  19. struct Data
  20. {
  21.     int x,y,sx,sy;
  22. };
  23.  
  24.  
  25. /*
  26. ** AskMinMax method will be called before the window is opened
  27. ** and before layout takes place. We need to tell MUI the
  28. ** minimum, maximum and default size of our object.
  29. */
  30.  
  31. SAVEDS ULONG mAskMinMax(struct IClass *cl,Object *obj,struct MUIP_AskMinMax *msg)
  32. {
  33.     /*
  34.     ** let our superclass first fill in what it thinks about sizes.
  35.     ** this will e.g. add the size of frame and inner spacing.
  36.     */
  37.  
  38.     DoSuperMethodA(cl,obj,msg);
  39.  
  40.     /*
  41.     ** now add the values specific to our object. note that we
  42.     ** indeed need to *add* these values, not just set them!
  43.     */
  44.  
  45.     msg->MinMaxInfo->MinWidth  += 100;
  46.     msg->MinMaxInfo->DefWidth  += 120;
  47.     msg->MinMaxInfo->MaxWidth  += 500;
  48.  
  49.     msg->MinMaxInfo->MinHeight += 40;
  50.     msg->MinMaxInfo->DefHeight += 90;
  51.     msg->MinMaxInfo->MaxHeight += 300;
  52.  
  53.     return(0);
  54. }
  55.  
  56.  
  57. /*
  58. ** Draw method is called whenever MUI feels we should render
  59. ** our object. This usually happens after layout is finished
  60. ** or when we need to refresh in a simplerefresh window.
  61. ** Note: You may only render within the rectangle
  62. **       _mleft(obj), _mtop(obj), _mwidth(obj), _mheight(obj).
  63. */
  64.  
  65. SAVEDS ULONG mDraw(struct IClass *cl,Object *obj,struct MUIP_Draw *msg)
  66. {
  67.     struct Data *data = INST_DATA(cl,obj);
  68.  
  69.     /*
  70.     ** let our superclass draw itself first, area class would
  71.     ** e.g. draw the frame and clear the whole region. What
  72.     ** it does exactly depends on msg->flags.
  73.     */
  74.  
  75.     DoSuperMethodA(cl,obj,msg);
  76.  
  77.     /*
  78.     ** if MADF_DRAWOBJECT isn't set, we shouldn't draw anything.
  79.     ** MUI just wanted to update the frame or something like that.
  80.     */
  81.  
  82.     if (msg->flags & MADF_DRAWUPDATE) /* called from our input method */
  83.     {
  84.         if (data->sx || data->sy)
  85.         {
  86.             SetBPen(_rp(obj),_dri(obj)->dri_Pens[SHINEPEN]);
  87.             ScrollRaster(_rp(obj),data->sx,data->sy,_mleft(obj),_mtop(obj),_mright(obj),_mbottom(obj));
  88.             SetBPen(_rp(obj),0);
  89.             data->sx = 0;
  90.             data->sy = 0;
  91.         }
  92.         else
  93.         {
  94.             SetAPen(_rp(obj),_dri(obj)->dri_Pens[SHADOWPEN]);
  95.             WritePixel(_rp(obj),data->x,data->y);
  96.         }
  97.     }
  98.     else if (msg->flags & MADF_DRAWOBJECT)
  99.     {
  100.         SetAPen(_rp(obj),_dri(obj)->dri_Pens[SHINEPEN]);
  101.         RectFill(_rp(obj),_mleft(obj),_mtop(obj),_mright(obj),_mbottom(obj));
  102.     }
  103.  
  104.     return(0);
  105. }
  106.  
  107.  
  108. SAVEDS ULONG mSetup(struct IClass *cl,Object *obj,struct MUIP_HandleInput *msg)
  109. {
  110.     if (!(DoSuperMethodA(cl,obj,msg)))
  111.         return(FALSE);
  112.  
  113.     MUI_RequestIDCMP(obj,IDCMP_MOUSEBUTTONS|IDCMP_RAWKEY);
  114.  
  115.     return(TRUE);
  116. }
  117.  
  118.  
  119. SAVEDS ULONG mCleanup(struct IClass *cl,Object *obj,struct MUIP_HandleInput *msg)
  120. {
  121.     MUI_RejectIDCMP(obj,IDCMP_MOUSEBUTTONS|IDCMP_RAWKEY);
  122.     return(DoSuperMethodA(cl,obj,msg));
  123. }
  124.  
  125.  
  126. SAVEDS ULONG mHandleInput(struct IClass *cl,Object *obj,struct MUIP_HandleInput *msg)
  127. {
  128.     #define _between(a,x,b) ((x)>=(a) && (x)<=(b))
  129.     #define _isinobject(x,y) (_between(_mleft(obj),(x),_mright(obj)) && _between(_mtop(obj),(y),_mbottom(obj)))
  130.  
  131.     struct Data *data = INST_DATA(cl,obj);
  132.  
  133.     if (msg->muikey)
  134.     {
  135.         switch (msg->muikey)
  136.         {
  137.             case MUIKEY_LEFT : data->sx=-1; MUI_Redraw(obj,MADF_DRAWUPDATE); break;
  138.             case MUIKEY_RIGHT: data->sx= 1; MUI_Redraw(obj,MADF_DRAWUPDATE); break;
  139.             case MUIKEY_UP   : data->sy=-1; MUI_Redraw(obj,MADF_DRAWUPDATE); break;
  140.             case MUIKEY_DOWN : data->sy= 1; MUI_Redraw(obj,MADF_DRAWUPDATE); break;
  141.         }
  142.     }
  143.  
  144.     if (msg->imsg)
  145.     {
  146.         switch (msg->imsg->Class)
  147.         {
  148.             case IDCMP_MOUSEBUTTONS:
  149.             {
  150.                 if (msg->imsg->Code==SELECTDOWN)
  151.                 {
  152.                     if (_isinobject(msg->imsg->MouseX,msg->imsg->MouseY))
  153.                     {
  154.                         data->x = msg->imsg->MouseX;
  155.                         data->y = msg->imsg->MouseY;
  156.                         MUI_Redraw(obj,MADF_DRAWUPDATE);
  157.                         MUI_RequestIDCMP(obj,IDCMP_MOUSEMOVE);
  158.                     }
  159.                 }
  160.                 else
  161.                     MUI_RejectIDCMP(obj,IDCMP_MOUSEMOVE);
  162.             }
  163.             break;
  164.  
  165.             case IDCMP_MOUSEMOVE:
  166.             {
  167.                 if (_isinobject(msg->imsg->MouseX,msg->imsg->MouseY))
  168.                 {
  169.                     data->x = msg->imsg->MouseX;
  170.                     data->y = msg->imsg->MouseY;
  171.                     MUI_Redraw(obj,MADF_DRAWUPDATE);
  172.                 }
  173.             }
  174.             break;
  175.         }
  176.     }
  177.  
  178.     return(0);
  179. }
  180.  
  181.  
  182. /*
  183. ** Here comes the dispatcher for our custom class. We only need to
  184. ** care about MUIM_AskMinMax and MUIM_Draw in this simple case.
  185. ** Unknown/unused methods are passed to the superclass immediately.
  186. */
  187.  
  188. SAVEDS ASM ULONG MyDispatcher(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  189. {
  190.     switch (msg->MethodID)
  191.     {
  192.         case MUIM_AskMinMax  : return(mAskMinMax  (cl,obj,(APTR)msg));
  193.         case MUIM_Draw       : return(mDraw       (cl,obj,(APTR)msg));
  194.         case MUIM_HandleInput: return(mHandleInput(cl,obj,(APTR)msg));
  195.         case MUIM_Setup      : return(mSetup      (cl,obj,(APTR)msg));
  196.         case MUIM_Cleanup    : return(mCleanup    (cl,obj,(APTR)msg));
  197.     }
  198.  
  199.     return(DoSuperMethodA(cl,obj,msg));
  200. }
  201.  
  202.  
  203.  
  204. /***************************************************************************/
  205. /* Thats all there is about it. Now lets see how things are used...        */
  206. /***************************************************************************/
  207.  
  208. int main(int argc,char *argv[])
  209. {
  210.     APTR app,window,MyObj,SuperClass;
  211.     struct IClass *MyClass;
  212.     ULONG signals;
  213.     BOOL running = TRUE;
  214.  
  215.     init();
  216.  
  217.     /* Get a pointer to the superclass. MUI will lock this */
  218.     /* and prevent it from being flushed during you hold   */
  219.     /* the pointer. When you're done, you have to call     */
  220.     /* MUI_FreeClass() to release this lock.               */
  221.  
  222.     if (!(SuperClass=MUI_GetClass(MUIC_Area)))
  223.         fail(NULL,"Superclass for the new class not found.");
  224.  
  225.     /* create the new class */
  226.     if (!(MyClass = MakeClass(NULL,NULL,SuperClass,sizeof(struct Data),0)))
  227.     {
  228.         MUI_FreeClass(SuperClass);
  229.         fail(NULL,"Failed to create class.");
  230.     }
  231.  
  232.     /* set the dispatcher for the new class */
  233.     MyClass->cl_Dispatcher.h_Entry    = (APTR)MyDispatcher;
  234.     MyClass->cl_Dispatcher.h_SubEntry = NULL;
  235.     MyClass->cl_Dispatcher.h_Data     = NULL;
  236.  
  237.     app = ApplicationObject,
  238.         MUIA_Application_Title      , "Class3",
  239.         MUIA_Application_Version    , "$VER: Class3 1.0 (01.12.93)",
  240.         MUIA_Application_Copyright  , "©1993, Stefan Stuntz",
  241.         MUIA_Application_Author     , "Stefan Stuntz",
  242.         MUIA_Application_Description, "Demonstrate the use of custom classes.",
  243.         MUIA_Application_Base       , "CLASS3",
  244.  
  245.         SubWindow, window = WindowObject,
  246.             MUIA_Window_Title, "A rather complex custom class",
  247.             MUIA_Window_ID   , MAKE_ID('C','L','S','3'),
  248.             WindowContents, VGroup,
  249.  
  250.                 Child, TextObject,
  251.                     TextFrame,
  252.                     MUIA_Background, MUII_TextBack,
  253.                     MUIA_Text_Contents, "\33cPaint with mouse,\nscroll with cursor keys.", 
  254.                     End,
  255.  
  256.                 Child, MyObj = NewObject(MyClass,NULL,
  257.                     TextFrame,
  258.                     TAG_DONE),
  259.  
  260.                 End,
  261.  
  262.             End,
  263.         End;
  264.  
  265.     if (!app)
  266.         fail(app,"Failed to create Application.");
  267.  
  268.     set(window,MUIA_Window_DefaultObject, MyObj);
  269.  
  270.     DoMethod(window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
  271.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  272.  
  273.  
  274.  
  275. /*
  276. ** Input loop...
  277. */
  278.  
  279.     set(window,MUIA_Window_Open,TRUE);
  280.  
  281.     while (running)
  282.     {
  283.         switch (DoMethod(app,MUIM_Application_Input,&signals))
  284.         {
  285.             case MUIV_Application_ReturnID_Quit:
  286.                 running = FALSE;
  287.                 break;
  288.         }
  289.  
  290.         if (running && signals) Wait(signals);
  291.     }
  292.  
  293.     set(window,MUIA_Window_Open,FALSE);
  294.  
  295.  
  296. /*
  297. ** Shut down...
  298. */
  299.  
  300.     MUI_DisposeObject(app);     /* dispose all objects. */
  301.     FreeClass(MyClass);         /* free our custom class. */
  302.     MUI_FreeClass(SuperClass); /* release super class pointer. */
  303.     fail(NULL,NULL);            /* exit, app is already disposed. */
  304. }
  305.